package com.csh.szhm.adapter;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.csh.szhm.R;
import com.csh.szhm.SzhmApplication;
import com.csh.szhm.http.HttpUrls;
import com.csh.szhm.module.shop.ShopListItem;
import com.csh.szhm.utils.AppConfig;
import com.csh.szhm.utils.StringUtil;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class SzhmSortThreeAdapter extends MyBaseAdapter<ShopListItem> {

	public SzhmSortThreeAdapter(Context c) {
		super(c);
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		HolderView holderView = null;
		if (convertView == null) {
			holderView = new HolderView();
			convertView = mLayoutInflater.inflate(
					R.layout.szhm_sort_three_item_layout, null);
			holderView.img = (ImageView) convertView
					.findViewById(R.id.szhm_sort_three_1);
			holderView.txt1 = (TextView) convertView
					.findViewById(R.id.szhm_sort_three_2);
			holderView.txt2 = (TextView) convertView
					.findViewById(R.id.szhm_sort_three_3);
			holderView.bar = (RatingBar) convertView
					.findViewById(R.id.ratingbar);
			holderView.txt3 = (TextView) convertView
					.findViewById(R.id.szhm_sort_three_4);
			holderView.txt4 = (TextView) convertView
					.findViewById(R.id.szhm_sort_three_5);
			convertView.setTag(holderView);
		} else {
			holderView = (HolderView) convertView.getTag();
		}
		SzhmApplication.display(HttpUrls.PATH_ROOT
				+ mData.get(position).getFirmImagePath(), holderView.img);
		holderView.txt1.setText(mData.get(position).getFirmName());
		holderView.txt3.setText(mData.get(position).getFirmAddr());
		String dic = mData.get(position).getDiscount();
		if (StringUtil.isEmptyString(dic)) {
			holderView.txt2.setText("会员价");
		} else {
			holderView.txt2.setText(dic + "折");
		}
		String tatal = mData.get(position).getEvalNum();
		if (!StringUtil.isEmptyString(tatal)) {
			int temp = Integer.valueOf(tatal);
			holderView.bar.setRating(temp);
		} else {
			holderView.bar.setRating(0);
		}
		String lat = mData.get(position).getLttude();// 经度
		String log = mData.get(position).getLgtude();// 维度
		if (!StringUtil.isEmptyString(lat)) {
			double latt = Double.parseDouble(lat);
			double logg = Double.parseDouble(log);
			LatLng data = new LatLng(latt, logg);
			holderView.txt4.setText(AppConfig.doubleTwoNum(DistanceUtil
					.getDistance(SzhmApplication.mLatLng, data)) + "km");
		} else {
			holderView.txt4.setText("");
		}

		return convertView;
	}

	private class HolderView {
		ImageView img;
		TextView txt1;
		TextView txt2;
		RatingBar bar;
		TextView txt3;
		TextView txt4;
	}
}
